import Reflux from 'reflux';
import PageConst from './PageConst';
import Actions from './Actions';
import { Control } from 'react-keeper'
import update from 'react-addons-update';
import Utils from './../../../utils';
import moment from 'moment';
import DB from './../../../app/db';

export default Reflux.createStore({
  listenables: [Actions],
  data: {
    ...PageConst,
    //滚动组件
    infs: null,
    //查询到的日志
    rzls: [],
    //选中的日志
    currentObj: {},
    //选中工程
    currentEng: {},

    //搜索关键字
    keyword: '',
    //当前页数
    currentPage: 1,
    pageSize: 10,
    isUpload: false,
    lastPage: false,
    isStart: false,
    //本弹窗是否显示
    fullpage_show: false,
    jlrq:{},
    //工程id
    engid:'',
    //条件搜索
    searchText:'',
    items: []
  },



  onsetCurrentObj: function (params) {
    console.info('onsetCurrentObj');
    console.info(params);
    this.data.currentObj = update(this.data.currentObj, {$set: params});
    console.info('select');
    console.info(this.data.currentObj);
    this.updateComponent();
  },

  onGoInfo: function (obj) {
    console.info('onGoInfo');
    console.log('返回2');
    console.info(obj);
    Control.go('/myyewu/rizhiaq',obj);
  },

  onKeySearch: function (params) {
    let t = this;
    console.info('onKeySearch');
    console.info(params);
    this.data.lastPage = update(this.data.lastPage, {$set: true});
    this.data.currentPage = update(this.data.currentPage, {$set: 1});
    this.data.currentObj = update(this.data.currentObj, {$set: {}});
    this.data.rzls = update(this.data.rzls, {$set: []});
    this.data.items = update(this.data.items, {$set: []});


    this.data.keyword = update(this.data.keyword, {$set: params});
   // this.data.isStart = update(this.data.isStart, {$set: false});
    console.info('测试isstart');
    console.info(this.data.isStart);
    if(this.data.isStart)
    {
      console.info('测试isstart 进入'+this.data.isStart);
      this.onKeyFetch({
        page: 1,
        size: 50,
        keyword:'',
        userid:'13972608122',
        engid:'',
        rq:'',
      });
    }
    t.updateComponent();
  },

  //初始化方法
  onInit: function () {
    console.log("onInit  ",this.data.engid );
    this.data.engid = update(this.data.engid, {$set: ''});
    this.data.searchText = update(this.data.searchText, {$set: ''});

    console.log("onInit工程查看  ",this.data.engid );
    this.data.fullpage_show = update(this.data.fullpage_show, {$set: false});
    this.data.currentPage = update(this.data.currentPage, {$set: 1});
    this.data.currentObj = update(this.data.currentObj, {$set: {}});
    this.data.rzls = update(this.data.rzls, {$set: []});
    this.data.items = update(this.data.items, {$set: []});
    this.data.lastPage = update(this.data.lastPage, {$set: false});
    this.data.currentEng = update(this.data.currentEng, {$set: {}});
    this.data.isStart = update(this.data.isStart, {$set: false});

    console.log("001===>>>  ",moment().format('YYYY-MM-DD HH:mm:ss'));
    this.updateComponent();
  },

  initMode: function (params) {
    //this.data.currentEng = update(this.data.currentEng, {$set: params});
    let resultOid = Utils.isEmptyObject(params);
    if (!resultOid.success)
    {

    }
    else {
      console.log("监督记录  ");
      console.info(params);
      this.data.engid = update(this.data.engid, {$set: ''});
      this.data.items = update(this.data.items, {$set: params.reljl});
    }

    this.updateComponent();
  },


  loadMode: function (params) {
    let t = this;
    console.log("loadMode ");
    this.onInit();
    this.initMode(params);
    this.data.isStart = update(this.data.isStart, {$set: true});
   /* this.onKeyFetch({
      page: 1,
      size: 10,
      keyword:'',
      userid:'13972608122',
      engid:'',
      rq:'',
    });
   */
    this.updateComponent();
  },






  onJlFetch: function (params) {
    console.info('onJlFetch');
    let t = this;
    params.page = t.data.currentPage;
    params.size = t.data.pageSize;
    params.keyword = t.data.keyword;
    params.engid = t.data.engid;
    let resultRq = Utils.isEmptyObject(this.data.jlrq);
    console.info(resultRq.success);
    if (!resultRq.success)
    {
      params.rq = '';
      console.info('日期空');
      console.info(this.data.jlrq);
    }
    else
    {
      console.info('日期有');
    }

      if (!t.data.lastPage)
      {
        console.info('日期');
        console.info(t.data.jlrq);
        t.data.lastPage = update(t.data.lastPage, {$set: true});
        DB.WechatAPI.findAQRiZhiList(params).then(
         function (content) {
            if (content != null) {
              t.data.rzls = update(t.data.rzls, {$set: content.content});
              t.data.items = update(t.data.items, {$push: t.data.rzls});
              console.info('循环查询');
              console.info(t.data.rzls);
            }
            //如果请求后数据数量小于pageSize则设置为最后一页
            if (t.data.rzls.length < 10) {
              t.data.lastPage = update(t.data.lastPage, {$set: true});
            }
            else {
              t.data.lastPage = update(t.data.lastPage, {$set: false});
            }

        },
        function (error) {
          t.data.lastPage = update(t.data.lastPage, {$set: true});
          console.log(error);
        });
        t.updateComponent();
    }
  },

  onKeyFetch: function (params) {
    console.info('onKeyFetch');
    let t = this;
    params.page = t.data.currentPage;
    params.size = t.data.pageSize;
    params.keyword = t.data.keyword;
    params.engid = t.data.engid;
    let resultRq = Utils.isEmptyObject(this.data.jlrq);
    console.info(resultRq.success);
    if (!resultRq.success)
    {
      params.rq = '';
      console.info('日期空');
      console.info(this.data.jlrq);
    }
    else
    {
      console.info('日期有');
    }

    console.info('参数');
    console.info(params);
    DB.WechatAPI.findAQRiZhiList(params).then(
      function (content) {
        if (content != null) {
          console.info('日期有进入',content.content);
          t.data.items = update(t.data.items, {$set: content.content});
          console.info('日期有进入length',t.data.items.length);

          console.info('日期有进入size',t.data.items.size);
          //如果请求后数据数量小于pageSize则设置为最后一页
          if (t.data.items.length < 10) {
            t.data.lastPage = update(t.data.lastPage, {$set: true});
          }
        }
        t.updateComponent();
      },
      function (error) {
        console.log(error);
      });
  },

  onAddPage(){
    console.info('加载查询onAddPage');
    this.data.rzls = update(this.data.rzls, {$set: []});
    this.data.currentPage = update(this.data.currentPage,  {$set: this.data.currentPage+1});
    console.info('页数'+this.data.currentPage);
    this.onJlFetch({
      page: 1,
      size: 50,
      keyword:'',
      engid:'',
      userid:'13972608122',
      rq:'',
    });
    console.info('加载查询完毕');
  },

  onJlrqChange: function (params) {
    console.log('action_jlrqChange');
    this.data.lastPage = update(this.data.lastPage, {$set: true});
    this.data.jlrq = update(this.data.jlrq,  {$set: moment(params)});
    this.onKeyFetch({
      page: 1,
      size: 50,
      keyword:'',
      engid:'',
      userid:'',
      rq:params,
    });
    this.updateComponent();
  },

  onjlrqClear: function (params) {
    console.info('onjlrqClear');
    this.data.jlrq = update(this.data.jlrq,  {$set: {}});
    this.onJlrqChange(this.data.jlrq);
    console.info('日期');
    console.info(this.data.jlrq);
    this.onJlFetch({
      page: 1,
      size: 50,
      keyword:'',
      userid:'1397260812cc2',
      rq:'',
    });
    this.updateComponent();
  },






  onFetch: function (params, cb) {
    let t = this;
    t.updateComponent();
    cb && cb(t.data);
  },

  updateComponent: function () {
    this.trigger(this.data);
  },

  getInitialState: function () {
    return this.data;
  }


});
